using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace My.IdlConverter.LoadManager
{
    sealed class FileRenamer
    {
        public static readonly string FileExtra = "__";
        public static readonly char FileExtraCharacter = '_';

        public static string Rename(string fileName)
        {
            fileName = fileName.Trim();
            string newFileName = fileName;

            if (File.Exists(fileName))
            {
                newFileName = String.Format("{0}{1}", fileName, FileExtra);
                File.Move(fileName, newFileName);
            }

            return newFileName;
        }

        public static string Retain(string fileName)
        {
            fileName = fileName.Trim();
            string newFileName = fileName;

            if (File.Exists(fileName))
            {
                int index =fileName.IndexOf(FileExtra);
                if (index != -1)
                {
                    newFileName = fileName.TrimEnd(new char[] { FileExtraCharacter});
                    File.Move(fileName, newFileName);
                }
            }

            return newFileName;
        }
    }
}
